package com.cook.controller;

import com.cook.dto.user.PostDTO;
import com.cook.result.Result;
import com.cook.service.PostsService;
import com.cook.vo.user.PostVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/posts")
@Slf4j
public class PostsController {

    @Autowired
    private PostsService postsService;

    /**
     * 分页查询用户的动态列表
     * @param userId 用户ID
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @return 分页结果
     */
    @GetMapping("/user/{userId}")
    public Result<Map<String, Object>> getPostsByUserId(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        log.info("分页查询用户动态: userId={}, pageNum={}, pageSize={}", userId, pageNum, pageSize);

        try {
            Map<String, Object> result = postsService.getPostsByUserIdWithPage(userId, pageNum, pageSize);
            return Result.success(result);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (RuntimeException e) {
            log.warn("查询失败: {}", e.getMessage());
            return Result.error(404, e.getMessage());
        } catch (Exception e) {
            log.error("查询动态列表失败: ", e);
            return Result.error(500, "查询动态列表失败");
        }
    }

    /**
     * 分页查询所有动态列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/ports")
    public Result<Map<String, Object>> getAllPosts(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        log.info("分页查询用户动态: pageNum={}, pageSize={}",pageNum, pageSize);

        try {
            Map<String, Object> result = postsService.getAllPostsWithPage(pageNum, pageSize);
            return Result.success(result);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (RuntimeException e) {
            log.warn("查询失败: {}", e.getMessage());
            return Result.error(404, e.getMessage());
        } catch (Exception e) {
            log.error("查询动态列表失败: ", e);
            return Result.error(500, "查询动态列表失败");
        }
    }

    /**
     * 查询动态详情
     * @param postId 动态ID
     * @return 动态详情
     */
    @GetMapping("/{postId}")
    public Result<PostVO> getPostById(@PathVariable Long postId) {
        log.info("查询动态详情: postId={}", postId);

        try {
            PostVO post = postsService.getPostById(postId);
            return Result.success(post);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (RuntimeException e) {
            log.warn("查询失败: {}", e.getMessage());
            return Result.error(404, e.getMessage());
        } catch (Exception e) {
            log.error("查询动态详情失败: ", e);
            return Result.error(500, "查询动态详情失败");
        }
    }

    /**
     * 创建动态
     * @param postDTO 动态数据
     * @return 创建的动态ID
     */
    @PostMapping
    public Result<Long> createPost(@RequestBody PostDTO postDTO) {
        log.info("创建动态: userId={}", postDTO.getUserId());

        try {
            Long postId = postsService.createPost(postDTO);
            return Result.success("动态创建成功", postId);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("创建动态失败: ", e);
            return Result.error(500, "创建动态失败");
        }
    }

    /**
     * 删除动态
     * @param postId 动态ID
     * @param userId 用户ID
     * @return 是否删除成功
     */
    @DeleteMapping("/{postId}")
    public Result<Boolean> deletePost(@PathVariable Long postId, @RequestParam Long userId) {
        log.info("删除动态: postId={}, userId={}", postId, userId);

        try {
            boolean success = postsService.deletePost(postId, userId);
            if (success) {
                return Result.success("动态删除成功", true);
            } else {
                return Result.error(403, "删除失败或无权限");
            }
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("删除动态失败: ", e);
            return Result.error(500, "删除动态失败");
        }
    }

    /**
     * 查询用户的动态数量
     * @param userId 用户ID
     * @return 动态数量
     */
    @GetMapping("/user/{userId}/count")
    public Result<Integer> getPostsCount(@PathVariable Long userId) {
        log.info("查询用户动态数量: userId={}", userId);

        try {
            int count = postsService.getPostsCountByUserId(userId);
            return Result.success(count);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("查询动态数量失败: ", e);
            return Result.error(500, "查询动态数量失败");
        }
    }

    /**
     * 查询所有动态数量
     * @return
     */
    @GetMapping("/ports/count")
    public Result<Integer> getAllPostsCount() {
        log.info("查询所有动态数量");

        try {
            int count = postsService.getAllPostsCount();
            return Result.success(count);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("查询动态数量失败: ", e);
            return Result.error(500, "查询动态数量失败");
        }
    }
}